var uname=document.querySelector('#uname')
var pwd=document.getElementById("pwd")
var user_rp=document.getElementById("user_rp")
var phonenumber=document.getElementById("phonenumber")
var btn = document.getElementById("btn")
let i=0
window.onload= function (){
    btn.disabled=true;
}
uname.onblur = function () {
    var xhr = new XMLHttpRequest()
    var params ="name="+uname.value
    xhr.open("get","http://localhost:9001/queryCount?"+params,true)
    xhr.send()
    xhr.onload = function (){
        if (uname.value===''){
            document.getElementById("span1").setAttribute("hidden","hidden")
        }
        if (xhr.responseText===''){
            document.getElementById("span1").setAttribute("hidden","hidden")
        }
        var name = JSON.parse(xhr.responseText).uname
        if (name===uname.value){
            document.getElementById("span1").removeAttribute("hidden")
            btn.disabled=true;
        }

    }

    user_rp.onblur= function () {
        var pwdValue=pwd.value
        var user_rpValue=user_rp.value
        if (pwdValue===user_rpValue){
            document.getElementById("span2").setAttribute("hidden","hidden")
        }else{
            document.getElementById("span2").removeAttribute("hidden")
            btn.disabled=true;
        }

    }
}
btn.onclick = function (){
    var uname=document.querySelector('#uname').value
    var pwd=document.getElementById("pwd").value
    var phonenumber=document.getElementById("phonenumber").value
    var xhr = new XMLHttpRequest()
    var params ="uname="+uname+"&pwd="+pwd+"&phonenumber="+phonenumber
    xhr.open("get","http://localhost:9001/submit?"+params,true)
    xhr.send()
    xhr.onload = function (){
        switch (xhr.responseText){
            case"login" :
                window.location.href="http://localhost:8888"
        }
    }

}
